import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class RegisterWin extends JFrame implements ActionListener {
    JLabel label_Name,label_Password,label_Password2;
    JTextField name,password,password2;
    JButton submit,exit;
    Box baseBox,box1,box2,box3;
    Connection con;
    Statement sql;
    InterfaceWin interfaceWin;
    RegisterWin(String title){
        setTitle(title);
        label_Name=new JLabel("请输入账户名");
        label_Password=new JLabel("请输入密码");
        label_Password2=new JLabel("请再次输入密码");
        name=new JTextField(20);
        password=new JTextField(10);
        password2=new JTextField(10);
        submit=new JButton("注册");
        exit=new JButton("退出");

        setLayout(null);
        baseBox=Box.createHorizontalBox();
        box1=Box.createVerticalBox();
        box2=Box.createVerticalBox();
        box3=Box.createHorizontalBox();
        box1.add(label_Name);
        box1.add(Box.createVerticalStrut(30));
        box1.add(label_Password);
        box1.add(Box.createVerticalStrut(30));
        box1.add(label_Password2);
        box2.add(name);
        box2.add(Box.createVerticalStrut(20));
        box2.add(password);
        box2.add(Box.createVerticalStrut(20));
        box2.add(password2);
        baseBox.add(box1);
        baseBox.add(Box.createHorizontalStrut(10));
        baseBox.add(box2);
        add(baseBox);
        baseBox.setBounds(50,50,350,120);
        box3.add(submit);
        box3.add(Box.createHorizontalStrut(50));
        box3.add(exit);
        add(box3);
        box3.setBounds(130,180,350,50);

        submit.addActionListener(this);
        exit.addActionListener(this);

        setBounds(700,300,450,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent event){
        if(event.getSource()==submit){
            if(password.getText().trim()!=password2.getText().trim())
                JOptionPane.showMessageDialog(this,"两次输入的密码不一致","WARNING",JOptionPane.WARNING_MESSAGE);
            else {
                try {
                    con = DriverManager.getConnection("jdbc:sqlserver://localhost:49689;DatabaseName=identity", "sa", "061210");
                    sql = con.createStatement();
                    sql.executeUpdate("INSERT INTO Login VALUES('" + name.getText().trim() + "','" + password.getText().trim() + "')");
                    JOptionPane.showMessageDialog(this, "注册成功", "提示", JOptionPane.PLAIN_MESSAGE);
                    con.close();
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(this, e, "WARNING", JOptionPane.WARNING_MESSAGE);
                    System.exit(0);
                }
            }
        }
        else if(event.getSource()==exit){
            if(JOptionPane.showConfirmDialog(this,"是否退出","提示",JOptionPane.YES_NO_CANCEL_OPTION)==JOptionPane.YES_OPTION) {
                dispose();
                interfaceWin = new InterfaceWin();
            }
        }
    }
}
